﻿using ComponentFactory.Krypton.Toolkit;
using EasyERP_BLL;
using EasyERP_BLL.Base;
using EasyERP_CL;
using EasyERP_Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace EasyERP_View.FrmBase
{
    /// <summary>
    /// 往来单位窗体
    /// </summary>
    public partial class FrmPartner : DockContent
    {
        /// <summary>
        /// 声明往来单位业务逻辑对象
        /// </summary>
        private readonly B_Partner_BLL Partner_BLL = new B_Partner_BLL();

        /// <summary>
        /// 声明子代码业务逻辑对象
        /// </summary>
        private readonly B_Minor_BLL Minor_BLL = new B_Minor_BLL();

        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        public FrmPartner()
        {
            InitializeComponent();

        }
        #endregion

        #region 窗体加载事件
        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmPartner_Load(object sender, EventArgs e)
        {
            B_Minor model = new B_Minor();
            model.major_cd = "1002";

            #region ComboBox绑定银行名称
            DataTable dt = Minor_BLL.Get_Minor_nm(model);
            Cmb_Bank_cd.DataSource = dt;
            Cmb_Bank_cd.ValueMember = "minor_cd";
            Cmb_Bank_cd.DisplayMember = "minor_nm";
            #endregion

            #region ComboBox绑定付款方式
            model.major_cd = "1001";
            dt = Minor_BLL.Get_Minor_nm(model);
            Cmb_Pay_Type.DataSource = dt;
            Cmb_Pay_Type.ValueMember = "minor_cd";
            Cmb_Pay_Type.DisplayMember = "minor_nm";
            #endregion
        } 
        #endregion

        #region 单击删除按钮事件
        /// <summary>
        /// 单击删除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnDel_Click(object sender, EventArgs e)
        {
            B_Partner model = new B_Partner();
            model.bp_cd = Txt_Bp_cd.Text;

            if (B_Message_BLL.MessageYesOrNo("2101") == DialogResult.Yes)
            {
                if (Partner_BLL.Delete_B_Partner(model))
                {
                    Query();
                    B_Message_BLL.MessageCaveat("1005");
                }
            }
            Query();
        } 
        #endregion

        #region 单击新建按钮事件
        /// <summary>
        /// 单击新建按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnAdd_Click(object sender, EventArgs e)
        {
            //TlpBpInfo中所有的控件
            for (int i = 0; i < TlpBpInfo.Controls.Count; i++)
            {
                //判断是否是EasyERP_TextBox
                if (TlpBpInfo.Controls[i].GetType().Name == "EasyERP_TextBox")      //是EasyERP_TextBox
                {
                    //类型转换
                    EasyERP_TextBox textBox = (EasyERP_TextBox)TlpBpInfo.Controls[i];
                    //清空控件
                    textBox.Clear();
                }

                //判断是否是KryptonCheckBox
                if (TlpBpInfo.Controls[i].GetType().Name == "KryptonCheckBox")      //是KryptonCheckBox
                {
                    KryptonCheckBox checkBox = (KryptonCheckBox)TlpBpInfo.Controls[i];
                    //取消选中
                    checkBox.Checked = false;
                }

                //判断是否是KryptonComboBox
                if (TlpBpInfo.Controls[i].GetType().Name == "KryptonComboBox")      //是KryptonComboBox
                {
                    KryptonComboBox comboBox = (KryptonComboBox)TlpBpInfo.Controls[i];
                    //清空文字
                    comboBox.Text = string.Empty;
                }
            }
            Txt_Bp_cd.Enabled = true;
            //设置焦点
            Txt_Bp_cd.Focus();
        }
        #endregion

        #region 单击查询按钮事件
        /// <summary>
        /// 单击查询按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnSearch_Click(object sender, EventArgs e)
        {
            Query();
            Txt_Bp_cd.Enabled = true;
        }
        #endregion

        #region 单击单元格事件
        /// <summary>
        /// 单击单元格事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DGView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //获取选中表格内容
            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                Txt_Bp_cd.Text = DGView[bp_cd.Name, e.RowIndex].Value.ToString();
                Txt_Bp_Full_nm.Text = DGView[bp_full_nm.Name, e.RowIndex].Value.ToString();
                Txt_Bp_nm.Text = DGView[bp_nm.Name, e.RowIndex].Value.ToString();
                Chk_So_Flag.Checked = DGView[so_flag.Name, e.RowIndex].Value.ToString() == "Y";
                Chk_Po_Flag.Checked = DGView[po_flag.Name, e.RowIndex].Value.ToString() == "Y";
                Txt_Bp_Reper.Text = DGView[bp_reper.Name, e.RowIndex].Value.ToString();
                Txt_Bp_Addr.Text = DGView[bp_addr.Name, e.RowIndex].Value.ToString();
                Txt_Bp_Tel.Text = DGView[bp_tel.Name, e.RowIndex].Value.ToString();
                Txt_Bp_Tax.Text = DGView[bp_tax.Name, e.RowIndex].Value.ToString();
                Txt_Bank_Acct_No.Text = DGView[bank_acct_no.Name, e.RowIndex].Value.ToString();
                Txt_Bp_Email.Text = DGView[bp_email.Name, e.RowIndex].Value.ToString();
                Txt_Remark.Text = DGView[remark.Name, e.RowIndex].Value.ToString();
                Cmb_Bank_cd.SelectedValue = DGView[bank_cd.Name, e.RowIndex].Value;
                Cmb_Pay_Type.SelectedValue = DGView[pay_type.Name, e.RowIndex].Value;

                Txt_Bp_cd.Enabled = false;
            }
        }
        #endregion

        #region 往来单位信息查询方法
        /// <summary>
        /// 往来单位信息查询方法
        /// </summary>
        private void Query()
        {
            //数据封装
            B_Partner model = new B_Partner();
            if (RdoPo.Checked)
                model.so_flag = "p";
            else if (RdoSo.Checked)
                model.so_flag = "s";
            else
                model.so_flag = "Y";
            model.bp_full_nm = Query_TxtBp_Full_nm.Text.Trim();
            model.bp_reper = Query_TxtBp_Repre.Text.Trim();


            DataTable dt = Partner_BLL.Get_B_Partner(model);

            //绑定DataGridView
            DGView.DataSource = dt;

            if (dt.Rows.Count == 0)
            {
                B_Message_BLL.MessageCaveat("1004");
            }
        }

        #endregion

        #region 单击保存按钮事件
        /// <summary>
        /// 单击保存按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnSave_Click(object sender, EventArgs e)
        {
            //封装数据
            B_Partner model = new B_Partner
            {
                bp_cd = Txt_Bp_cd.Text.Trim(),
                bp_full_nm = Txt_Bp_Full_nm.Text.Trim(),
                bp_nm = Txt_Bp_nm.Text.Trim(),
                bp_reper = Txt_Bp_Reper.Text.Trim(),
                bp_addr = Txt_Bp_Addr.Text.Trim(),
                bp_tel = Txt_Bp_Tel.Text.Trim(),
                bp_tax = Txt_Bp_Tax.Text.Trim(),
                bank_acct_no = Txt_Bank_Acct_No.Text.Trim(),
                bp_email = Txt_Bp_Email.Text.Trim(),
                remark = Txt_Remark.Text.Trim(),
                bank_cd = Cmb_Bank_cd.SelectedValue.ToString(),
                pay_type = Cmb_Pay_Type.SelectedValue.ToString(),
                so_flag = Chk_So_Flag.Checked ? "Y" : "N",
                po_flag = Chk_Po_Flag.Checked ? "Y" : "N",
                user_cd = FrmMain.user_cd
            };

            //判断新增还是修改
            if (Txt_Bp_cd.Enabled == true)      //新增
            {
                if (Partner_BLL.Insert_B_Partner(model))
                {
                    Query();
                    B_Message_BLL.MessageCaveat("1005");
                }
            }
            else                                //修改
            {
                if (Partner_BLL.UpDate_B_Partner(model))
                {
                    Query();
                    B_Message_BLL.MessageCaveat("1005");
                }
            }
        }
        #endregion
    }
}
